package com.example.comiccenter.service.implt;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.comiccenter.dao.UserMapper;
import com.example.comiccenter.domain.Comic;
import com.example.comiccenter.domain.User;
import com.example.comiccenter.service.UserService;
import com.example.comiccenter.utils.JWTUtil;
import com.example.comiccenter.utils.ResultData;
import com.example.comiccenter.utils.SearchBean;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper usermapper;

    public ResultData insertUser(User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",user.getUsername());
        Long userNumber = usermapper.selectCount(userQueryWrapper);
        if (userNumber>0){
            return ResultData.error(400,"该账户已存在");
        }

        if (user.getName()==null){
            user.setName("用户");
        }
        int insert = usermapper.insert(user);
        if (insert == 1){
            return ResultData.success("新建成功");
        }else {
            return ResultData.error(400,"新建失败");
        }
    }


    //    删除
    public ResultData deleteUser(String username){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",username);
        int delete = usermapper.delete(userQueryWrapper);
        if (delete == 1){
            return ResultData.success("删除成功");
        }else {
            return ResultData.error(400,"该账户不存在");
        }
    }
    //    修改
    public ResultData updateUser(User user){
        int update = usermapper.updateById(user);
        if (update == 1){
            return ResultData.success("更新成功");
        }else {
            return ResultData.error(400,"更新失败");
        }
    }
    //    加载
    public ResultData loadAAllUsers(SearchBean<User> searchBean){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        System.out.println(searchBean);
        wrapper
                .eq(searchBean.getData().getUsername()!=null,"username",searchBean.getData().getUsername());
        Page<User> userPage = usermapper.selectPage(new Page<>(searchBean.getPageNum(), searchBean.getPageSize()), wrapper);
        return ResultData.success("查询成功",userPage);
    }


    public ResultData userLogin(User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        User user1 = usermapper.selectOne(wrapper);
        if (user1 == null){
            return ResultData.error(400,"该账户不存在");
        }
        if (!user.getPassword().equals(user1.getPassword())){
            return ResultData.error(400,"账户或密码错误");
        }
        String token = JWTUtil.getToken(user.getUsername());
        Map<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user1);
        return ResultData.success("登录成功",map);
    }
}
